home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 2.2 KB | 125 lines | [TEXT/CWIE] |
- // MenuBar.cp
-
- #ifndef MenuBar_h
- #include "MenuBar.h"
- #endif
- #ifndef ListLoop_h
- #include "ListLoop.h"
- #endif
- #ifndef Menu_h
- #include "Menu.h"
- #endif
- #ifndef Tick_h
- #include "Tick.h"
- #endif
- #ifndef UserState_h
- #include "UserState.h"
- #endif
-
- MenuBar::MenuBar()
- : listenToUserState( UserState::The(), *this, &MenuBar::Prepare )
- {
- }
-
- MenuBar& MenuBar::The()
- {
- static MenuBar the;
- return the;
- }
-
- void MenuBar::Add( ListLink<Menu>& link, AfterEnd )
- {
- MenuBarObject::Add( *link, afterEnd );
- mainMenus.Add( link, afterEnd );
- Invalidate();
- }
-
- void MenuBar::Add( ListLink<Menu>& link, AsSecondaryMenu as )
- {
- MenuBarObject::Add( *link, as );
- secondaryMenus.Add( link, afterEnd );
- }
-
- void MenuBar::Add( ListLink<Menu>& link,
- After,
- const ListLink<Menu>& previous )
- {
- MenuBarObject::Add( *link, after, *previous );
- mainMenus.Add( link, after, previous );
- Invalidate();
- }
-
- void MenuBar::Prepare()
- {
- for ( ListLoop<Menu> menu( secondaryMenus );
- menu.Unfinished();
- menu++ )
- menu->Prepare();
-
- bool updateBar = false;
-
- for ( ListLoop<Menu> menu( mainMenus );
- menu.Unfinished();
- menu++ )
- {
- bool wasEnabled = menu->Enabled();
- menu->Prepare();
- if ( menu->Enabled() != wasEnabled )
- updateBar = true;
- }
-
- if ( updateBar )
- DrawMenuBar();
- }
-
- Menu& MenuBar::operator[]( MenuID id ) const
- {
- Assert( id != 0 );
-
- for ( ListLoop<Menu> menu( mainMenus );
- menu.Unfinished();
- menu++ )
- if ( menu->ID() == id )
- return *menu;
-
- for ( ListLoop<Menu> menu( secondaryMenus );
- menu.Unfinished();
- menu++ )
- if ( menu->ID() == id )
- return *menu;
-
- Assert( 0 );
- return **mainMenus.First();
- }
-
- void MenuBar::Choose( MenuResult target )
- {
- if ( target.Null() )
- return;
-
- Tick stopHighlight( Tick::Now() + 3 );
-
- (*this)[ target.Menu() ].Choose( target.Item() );
-
- // Delay so that the user can see the menu title flash
- stopHighlight.WaitFor();
-
- HighlightNone();
- }
-
- void MenuBar::Click( Point p )
- {
- listenToUserState.Flush();
- MenuResult where( MenuBarObject::Click( p ) );
- Choose( where );
- }
-
- void MenuBar::Key( ::Key k )
- {
- listenToUserState.Flush();
- Choose( MenuBarObject::Key( k ) );
- }
-
- #include "ListOf.cp"
- #include "ListLoop.cp"
-